package com.mis.geoEncryption.model.data;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.mis.geoEncryption.model.connect.DatabaseConnector;

public class ItemExtractor implements IItemExtractor {
	
	public static String ITEMQUERYSTRING = "SELECT * FROM Item";
	public static String ITEMID_COLUMN = "itemID";
	public static String MERCHANTID_COLUMN = "merchantID";
	public static String ITEMNAME_COLUMN = "itemName";
	public static String NOOFITEMS_COLUMN = "noOfItems";
	public static String PRICE_COLUMN = "price";
	public static String MERCHANTNAME_COLUMN = "merchantName";
	
	private DatabaseConnector connector;
	
	public ItemExtractor(DatabaseConnector connector) {
		super();
		this.connector = connector;
	}

	public List<Item> getItemData() {
		List<Item> itemList = new ArrayList<Item>();
		try {
			ResultSet resultSet = connector.runSql(ItemExtractor.ITEMQUERYSTRING);
			while(resultSet.next()) {
				Item currentIem = new Item();
				currentIem.setItemID(resultSet.getString(ItemExtractor.ITEMID_COLUMN));
				currentIem.setMerchantID(resultSet.getString(ItemExtractor.MERCHANTID_COLUMN));
				currentIem.setItemName(resultSet.getString(ItemExtractor.ITEMNAME_COLUMN));
				currentIem.setPrice(resultSet.getString(ItemExtractor.PRICE_COLUMN));
				currentIem.setMerchantID(resultSet.getString(ItemExtractor.MERCHANTID_COLUMN));
				currentIem.setMerchantName(resultSet.getString(ItemExtractor.MERCHANTNAME_COLUMN));
				
				itemList.add(currentIem);
			}
			
		} catch(Exception e) {
			System.out.println("Error");
		}
		return itemList;
	}
	
}
